package com.tang.dict;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tang.system.vo.Result;
import com.tang.utils.CommonUtils;

import java.util.Collection;
import java.util.Map;

public abstract class DictParseAble {
    protected Object parseResult(Result obj) {
        obj.setResult(doParseDict(obj.getResult()));
        return obj;
    }

    protected Object parseMap(Map obj) {
        JSONObject jsonObject = new JSONObject();
        for (Object key : obj.keySet()) {
            if (CommonUtils.isEmpty(key)) continue;
            jsonObject.put(key.toString(), doParseDict(obj.get(key)));
        }
        return jsonObject;
    }

    protected Object parseCollection(Collection obj) {
        JSONArray array = new JSONArray();
        for (Object o : obj) {
            array.add(doParseDict(o));
        }
        return array;
    }

    protected Object parseArray(Object[] obj) {
        JSONArray array = new JSONArray();
        for (Object o : obj) {
            array.add(doParseDict(o));
        }
        return array;
    }

    protected Object parseIPage(IPage obj) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("pages", obj.getPages());
        jsonObject.put("current", obj.getCurrent());
        jsonObject.put("records", doParseDict(obj.getRecords()));
        jsonObject.put("size", obj.getSize());
        jsonObject.put("total", obj.getTotal());
        return jsonObject;
    }

    protected abstract Object parseDict(Object obj);

    protected abstract Object doParseDict(Object obj);
}
